-- ========================================================================
-- sakila database example queries
-- ========================================================================
-- Sakila is a DVD-rental store, modelled here as a fully RELATIONAL CoraDB
-- database (15 tables, standard ANSI SQL — joins, GROUP BY, subqueries).
-- Core links: rental -> inventory -> film; rental/payment -> customer;
-- inventory -> store; film <-> actor / category; customer/staff/store -> address
-- -> city -> country.
-- ========================================================================

------------------------------------------------------------------------
-- Basics
------------------------------------------------------------------------

-- How many films, actors, customers, rentals and payments are there?
SELECT COUNT(*) AS films FROM film;
SELECT COUNT(*) AS actors FROM actor;
SELECT COUNT(*) AS customers FROM customer;
SELECT COUNT(*) AS rentals FROM rental;
SELECT COUNT(*) AS payments FROM payment;

-- All films in the "Comedy" category, alphabetically.
SELECT f.title, f.release_year, f.rating
FROM film f
JOIN film_category fc ON fc.film_id = f.film_id
JOIN category c ON c.category_id = fc.category_id
WHERE c.name = 'Comedy'
ORDER BY f.title;

-- The films actor PENELOPE GUINESS appeared in.
SELECT f.title
FROM actor a
JOIN film_actor fa ON fa.actor_id = a.actor_id
JOIN film f ON f.film_id = fa.film_id
WHERE a.first_name = 'PENELOPE' AND a.last_name = 'GUINESS'
ORDER BY f.title;

------------------------------------------------------------------------
-- Films & actors
------------------------------------------------------------------------

-- How many films are in each category?
SELECT c.name AS category, COUNT(*) AS films
FROM category c
JOIN film_category fc ON fc.category_id = c.category_id
GROUP BY c.name
ORDER BY films DESC;

-- The 10 actors who appear in the most films.
SELECT a.first_name, a.last_name, COUNT(*) AS films
FROM actor a
JOIN film_actor fa ON fa.actor_id = a.actor_id
GROUP BY a.actor_id, a.first_name, a.last_name
ORDER BY films DESC
LIMIT 10;

-- The 10 longest films (with their rating and rental rate).
SELECT title, length, rating, rental_rate
FROM film
ORDER BY length DESC
LIMIT 10;

-- Average rental rate and film count per rating.
SELECT rating, COUNT(*) AS films, AVG(rental_rate) AS avg_rate, AVG(length) AS avg_length
FROM film
GROUP BY rating
ORDER BY films DESC;

-- Films that have never been rented (in the catalogue but no rental).
SELECT f.film_id, f.title
FROM film f
WHERE NOT EXISTS (
  SELECT 1 FROM inventory i
  JOIN rental r ON r.inventory_id = i.inventory_id
  WHERE i.film_id = f.film_id)
ORDER BY f.title;

------------------------------------------------------------------------
-- Rentals & inventory
------------------------------------------------------------------------

-- The 10 most-rented films.
SELECT f.title, COUNT(*) AS rentals
FROM rental r
JOIN inventory i ON i.inventory_id = r.inventory_id
JOIN film f ON f.film_id = i.film_id
GROUP BY f.film_id, f.title
ORDER BY rentals DESC
LIMIT 10;

-- Inventory copies held by each store.
SELECT store_id, COUNT(*) AS copies
FROM inventory
GROUP BY store_id;

-- Rentals still outstanding (not yet returned).
SELECT COUNT(*) AS out_now
FROM rental
WHERE return_date IS NULL;

-- Rentals per calendar month. (Use TO_CHAR()/YEAR() on dates: SUBSTR() on a DATETIME
-- works on its default text form '10:53:30.000 PM 05/24/2005', not 'YYYY-MM-DD'.)
SELECT TO_CHAR(rental_date, 'YYYY-MM') AS `month`, COUNT(*) AS rentals
FROM rental
GROUP BY TO_CHAR(rental_date, 'YYYY-MM')
ORDER BY `month`;

------------------------------------------------------------------------
-- Revenue & customers
------------------------------------------------------------------------

-- Total revenue per store.
SELECT s.store_id, SUM(p.amount) AS revenue
FROM payment p
JOIN rental r ON r.rental_id = p.rental_id
JOIN inventory i ON i.inventory_id = r.inventory_id
JOIN store s ON s.store_id = i.store_id
GROUP BY s.store_id
ORDER BY revenue DESC;

-- Total revenue per film category.
SELECT c.name AS category, SUM(p.amount) AS revenue
FROM payment p
JOIN rental r ON r.rental_id = p.rental_id
JOIN inventory i ON i.inventory_id = r.inventory_id
JOIN film_category fc ON fc.film_id = i.film_id
JOIN category c ON c.category_id = fc.category_id
GROUP BY c.name
ORDER BY revenue DESC;

-- The 10 top-spending customers.
SELECT cu.first_name, cu.last_name, SUM(p.amount) AS total_spent, COUNT(*) AS payments
FROM payment p
JOIN customer cu ON cu.customer_id = p.customer_id
GROUP BY cu.customer_id, cu.first_name, cu.last_name
ORDER BY total_spent DESC
LIMIT 10;

-- Revenue collected by each staff member.
SELECT st.first_name, st.last_name, SUM(p.amount) AS collected, COUNT(*) AS payments
FROM payment p
JOIN staff st ON st.staff_id = p.staff_id
GROUP BY st.staff_id, st.first_name, st.last_name
ORDER BY collected DESC;

-- Payments taken in July 2005.
SELECT COUNT(*) AS payments, SUM(amount) AS total
FROM payment
WHERE payment_date >= '2005-07-01' AND payment_date < '2005-08-01';

------------------------------------------------------------------------
-- Geography
------------------------------------------------------------------------

-- Number of customers per country (top 10).
SELECT co.country, COUNT(*) AS customers
FROM customer cu
JOIN address a ON a.address_id = cu.address_id
JOIN city ci ON ci.city_id = a.city_id
JOIN country co ON co.country_id = ci.country_id
GROUP BY co.country
ORDER BY customers DESC
LIMIT 10;

-- A store's location, resolved through the address / city / country chain.
SELECT s.store_id, a.address, a.district, ci.city, co.country
FROM store s
JOIN address a ON a.address_id = s.address_id
JOIN city ci ON ci.city_id = a.city_id
JOIN country co ON co.country_id = ci.country_id
ORDER BY s.store_id;

------------------------------------------------------------------------
-- More analytics (Sakila project set)
------------------------------------------------------------------------
-- A set of analytical questions adapted from the "MySQL Sakila Project"
-- walkthrough (Ramil Khalilli, Medium). Filtering, pattern matching,
-- aggregation with HAVING, correlated subqueries and per-customer metrics.

-- Customers living in the 'California' district (name, address, phone).
SELECT cu.first_name, cu.last_name, a.address, a.phone
FROM customer cu
JOIN address a ON a.address_id = cu.address_id
WHERE a.district = 'California'
ORDER BY cu.last_name;

-- Actors whose last name starts with 'D'.
SELECT first_name, last_name
FROM actor
WHERE last_name LIKE 'D%'
ORDER BY last_name, first_name;

-- Films that can be rented for more than 5 days (longest rental windows).
SELECT title, rental_duration
FROM film
WHERE rental_duration > 5
ORDER BY rental_duration DESC, title
LIMIT 10;

-- The most expensive films to replace (replacement_cost > 19.99).
SELECT title, replacement_cost, rating
FROM film
WHERE replacement_cost > 19.99
ORDER BY replacement_cost DESC, title
LIMIT 10;

-- The 5 films with the highest rental rate.
SELECT title, release_year, rental_rate
FROM film
ORDER BY rental_rate DESC, title
LIMIT 5;

-- The average rental duration across the whole catalogue.
SELECT AVG(rental_duration) AS avg_rental_days
FROM film;

-- The most active customers: those with more than 30 rentals.
SELECT cu.first_name, cu.last_name, COUNT(*) AS rentals
FROM rental r
JOIN customer cu ON cu.customer_id = r.customer_id
GROUP BY cu.customer_id, cu.first_name, cu.last_name
HAVING COUNT(*) > 30
ORDER BY rentals DESC;

-- Full rental history for customer 1 (films they rented, with dates).
SELECT f.title, r.rental_date, r.return_date
FROM rental r
JOIN inventory i ON i.inventory_id = r.inventory_id
JOIN film f ON f.film_id = i.film_id
WHERE r.customer_id = 1
ORDER BY r.rental_date;

-- Versatile actors: those who appear in films of 3 or more categories.
SELECT a.first_name, a.last_name, COUNT(DISTINCT fc.category_id) AS categories
FROM actor a
JOIN film_actor fa ON fa.actor_id = a.actor_id
JOIN film_category fc ON fc.film_id = fa.film_id
GROUP BY a.actor_id, a.first_name, a.last_name
HAVING COUNT(DISTINCT fc.category_id) >= 3
ORDER BY categories DESC, a.last_name
LIMIT 10;

-- Total payments collected per calendar year.
SELECT YEAR(payment_date) AS `year`, SUM(amount) AS total
FROM payment
GROUP BY YEAR(payment_date)
ORDER BY `year`;

-- Customer lifetime value: rentals taken and total money spent (top 10).
-- Payments are joined through their rental (not just the customer), otherwise every
-- payment is repeated once per rental and the totals are multiplied.
SELECT cu.first_name, cu.last_name,
       COUNT(r.rental_id) AS rentals,
       SUM(p.amount) AS total_spent
FROM customer cu
JOIN rental r ON r.customer_id = cu.customer_id
LEFT JOIN payment p ON p.rental_id = r.rental_id
GROUP BY cu.customer_id, cu.first_name, cu.last_name
ORDER BY total_spent DESC
LIMIT 10;

-- Store comparison: inventory copies and customers registered at each store.
SELECT s.store_id,
       (SELECT COUNT(*) FROM inventory i WHERE i.store_id = s.store_id) AS copies,
       (SELECT COUNT(*) FROM customer cu WHERE cu.store_id = s.store_id) AS customers
FROM store s
ORDER BY s.store_id;

-- Copies currently on the shelf (inventory with no open rental).
SELECT COUNT(*) AS available_copies
FROM inventory i
WHERE NOT EXISTS (
  SELECT 1 FROM rental r
  WHERE r.inventory_id = i.inventory_id
    AND r.return_date IS NULL);

